package com.tenglong.teacher.ui.setting;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.tenglong.teacher.R;
import com.tenglong.teacher.data.resultInfo.ClientUserInfo;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.data.resultInfo.base.BaseResult;
import com.tenglong.teacher.data.resultInfo.base.ResultVerifier;
import com.tenglong.teacher.services.hdx.CreateRequestEntity;
import com.tenglong.teacher.ui.common.activity.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChangeSexActivity extends BaseActivity {

    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_sex);
        initView();
        setListeners();
    }

    private void setListeners() {
        findViewById(R.id.changeSex).setOnClickListener(this);
    }

    private void initView() {
        setTitle("修改性别");
        radioGroup = findViewById(R.id.radioGroup);
        ClientUserInfo info = DataState.getInstance().getUserInfo();
        if (info.getSex() == 1) {
            radioGroup.check(R.id.man);
        } else if (info.getSex() == 2) {
            radioGroup.check(R.id.woman);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.changeSex:
                if (radioGroup.getCheckedRadioButtonId() == -1) {
                    ToastUtils.showCenterToast("请选择性别");
                    return;
                }
                ClientUserInfo info = DataState.getInstance().getUserInfo();
                if (radioGroup.getCheckedRadioButtonId() == R.id.man) {
                    if (info.getSex() == 1) {
                        ToastUtils.showCenterToast("您已经是男性性别");
                        return;
                    }
                } else {
                    if (info.getSex() == 2) {
                        ToastUtils.showCenterToast("您已经是女性性别");
                        return;
                    }
                }
                modifySex(radioGroup.getCheckedRadioButtonId() == R.id.man ? 1 : 2);
                break;
        }
    }


    private void modifySex(int sex) {
        showProgressDialog(getString(R.string.sign_modifying));
        final ClientUserInfo userInfo = DataState.getInstance().getUserInfo();
        userInfo.setSex(sex);
        CreateRequestEntity.getWebService()
                .modifyUserInfo(userInfo)
                .enqueue(new Callback<BaseResult<ClientUserInfo>>() {
                    @Override
                    public void onResponse(Call<BaseResult<ClientUserInfo>> call, Response<BaseResult<ClientUserInfo>> response) {
                        dismissProgressDialog();
                        if (ResultVerifier.isSucceed(response)) {
                            setResult(Activity.RESULT_OK);
                            finish();
                        } else {
                            ToastUtils.showCenterToast(ResultVerifier.serceErrers(response));
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResult<ClientUserInfo>> call, Throwable t) {
                        ResultVerifier.systerErrers(t);
                        dismissProgressDialog();
                    }
                });
    }
}
